import time


class StatisticsTime:
    def __init__(self, parent, key):
        self.parent = parent
        self.key = key
        self.current = 0

    def start(self):
        self.current = time.time()

    def end(self):
        self.parent.__end__(self.key, time.time() - self.current)


class Statistics(dict):
    def __init__(self):
        super().__init__()

    def start(self, key):
        if key not in self:
            self[key] = 0
        st_time = StatisticsTime(self, key)
        st_time.start()
        return st_time

    def __end__(self, key, run_time):
        self[key] += run_time

    def print(self):
        for k, v in self.items():
            print("%s: %f" % (k, v))
            time.sleep(0.1)


